angular.module('mamApp').controller('mainController', ['$scope', '$state', 'InitManger', 'messageManager', function ($scope, $state, InitManger, messageManager) {

    $scope.$state = $state;
    $scope.userInfo = JSON.parse(localStorage.getItem('userInfo'));
    $scope.messageIsShow = true;
    if ($state.includes('main')) {
        messageManager.messageUnRead().then(function (result) {
            $scope.unReadMessageCount = result.data.unReadNum;
            setInterval(function () {
                if ($scope.unReadMessageCount) {
                    $scope.$apply($scope.messageIsShow = !$scope.messageIsShow)
                }
            }, 500)

        })

    }


    $scope.messageClick = function () {
        messageManager.messageAllRead().then(function (result) {

            if (result.data.c == "s") {
                $scope.messageIsShow = true;
                $scope.unReadMessageCount = 0;
            }
        })
    };

    function resizeContent() {
        $(document).ready(function () {
            $scope.bodyHeight =$(document.body).height();
            $scope.minHeight={'min-height':$scope.bodyHeight-124+"px"}
        })
    }

    $scope.$on('$stateChangeSuccess',
        function (event, toState, toParams, fromState, fromParams) {
            document.body.scrollTop=0;

            resizeContent();

            InitManger.menuItem().then(function (result) {
                $scope.meunItems = result.CP;
                $scope.meunItems.forEach(function (meun) {
                    meun.items.forEach(function (item1) {
                        if ($state.includes(item1.sref)) {
                            meun.open = true;
                        }
                    })
                })
            })
        })
    $(window).resize(function () {
        resizeContent();
    });


    if (!localStorage.getItem("provinces")) {
        InitManger.provinces().then(function (result) {
            localStorage.setItem("provinces", JSON.stringify(result))
        })

        InitManger.citys().then(function (result) {
            localStorage.setItem("city", JSON.stringify(result))

        })

    }


    InitManger.codeTable().then(function (result) {
        localStorage.setItem("codeTable", JSON.stringify(result))
        $scope.table = JSON.parse(localStorage.getItem('codeTable'));

    })

    $scope.menuTitleClick = function (item) {
        if (item.sref) {
            $state.go(item.sref, {}, {inherit: false})
        }

    }


}])

